<?php
abstract class Admin_BaseController extends Zend_Controller_Action{
	protected  $_redirector;
	private $_userModel;
	public function init(){
		$this->view->headTitle("AnThaiAuto - Administrator");
		$this->_redirector = $this->_helper->getHelper('Redirector');
	
		$this->_userModel = new Model_User();
	}
	public function authorize($authorizedRoles = null){
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			if($authorizedRoles != null){
				$arrAuthorizedRoles = (array) $authorizedRoles;
				$identity = $auth->getIdentity();
				$user = $this->_userModel->getUserByID($identity->id);
				$arrUserRoles = $user->UserRoles;
				foreach ($arrUserRoles as $userRole){
					$arrRoles[] = $userRole->Role->name;
				}
				$matched = 0;
				foreach ($arrAuthorizedRoles as $authorizedRole) {
					if(in_array($authorizedRole, $arrRoles)){
						$matched+=1;
					}	
				}
				if($matched!=0){
					//return Constain_Authorization::AUTHORIZED;
				}else{
					return Constain_Authorization::NOT_AUTHORIZED;
					$this->_redirect(Constain_SiteUrl::CANT_NOT_ACCESS);
				}
			}else{
				//return Constain_Authorization::AUTHORIZED;
			}
		} else{
			$this->_redirector->gotoRoute(array(),'admin_login');
		}
	}
}